#include<iostream>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
const int N=1e5+10;
vector<string> v;
int n;
string a,b;
int ret=1e5+10;
int main()
{

    cin>>n;
    cin>>a>>b;
    for(int i=1;i<=n;i++)
    {
        string x;
        cin>>x;
        v.push_back(x);
    }
    bool st1=false,st2=false;
    int at1=-1e5,at2=-1e5;
    for(int i=0;i<v.size();i++)
    {
        if(v[i]==a)
        {
            st1=true;
            at1=i;
            ret=min(ret,abs(at1-at2));
        }
        if(v[i]==b)
        {
            st2=true;
            at2=i;
            ret=min(ret,abs(at1-at2));
        }
    }
    if(st1==false||st2==false)
    {
         cout<<-1<<endl;
         return 0;
    }
    else {
        cout<<ret<<endl;
    }

    return 0;
}